<template>
  <div class="upload-btn">
    <el-upload
      class="upload-wrap"
      drag
      :headers="headers"
      :action="action"
      :accept="fileType"
      :multiple="multiple"
      :show-file-list="false"
      :http-request="action == '' ? handleUpload : undefined"
      :before-upload="beforeFileUpload"
      :on-success="handleSuccess"
      :on-error="handleError"
    >
      <div class="el-upload__text">
        <img class="ic-40" src="@/assets/common/upload_4.png" />
        <span>{{ loadLabel }}</span>
      </div>
    </el-upload>
  </div>
</template>

<script>
import Store from "@/store/index";
export default {
  name: "UploadBtn",
  props: {
    // 文件上传提示
    uploadTip: {
      type: String,
      default:
        "支持excel、pdf、doc、docx、txt、jpg、png等格式文件上传，文件大小不超过20MB，所有附件个数不超过9个",
    },
    action: {
      type: String,
      default: "",
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    limitNum: {
      type: Number,
      default: 9,
    },
    // 文件类型
    fileType: {
      type: String,
      default: ".jpg,.png",
    },
    // 文件限制大小
    maxFileSize: {
      type: Number,
      default: 20,
    },
    // value: {
    //   type: Array,
    //   default: () => [],
    //   required: true,
    // },
    loadLabel: {
      type: String,
      default: "上传图片",
    },
  },
  data: () => ({
    fileList: [],
    headers: {
      Authorization: "Bearer " + Store.state.user.token,
    },
  }),
  methods: {
    handleUpload() {},
    beforeFileUpload(file) {
      this.$message.closeAll();
      if (this.fileLenth >= this.limitNum) {
        this.$message.warning(`至多上传${this.limitNum}份附件！`);
        return false;
      }
      if (this.fileList.some((item) => item.fileName === file.name)) {
        this.$message.warning(`${file.name}已存在，请勿重复上传`);
        return false;
      }
      const fileType = file.name.split(".").pop();
      let fileTypeList = this.fileType
        .split(",")
        .map((item) => item.replace(/\./g, ""));
      if (!fileTypeList.includes(fileType)) {
        this.$message.error(`请上传${this.fileType}格式文件！`);
        return false;
      }
      const isMax = file.size / 1024 / 1024 < this.maxFileSize;
      if (!isMax) {
        this.$message.error(`上传文件大小不能超过${this.maxFileSize}M`);
        return false;
      }
      this.fileLenth += 1;
      if (!this.action) {
        file.fileName = file.name;
        this.fileList.push(file);
        // this.timeCountDown(file);
        this.emitFileHandle();
        this.$emit("upload-back", this.ftObject);
      } else {
        this.$load.show();
      }
    },
    handleSuccess(res, file) {
      console.log(res, ">>>>>>>>>>>.");
      this.$load.close();
      this.$emit("success", res.data);
      // res.data.fileName = res.data.originalName;

      // this.timeCountDown(file);
      // this.emitFileHandle();
    },
    emitFileHandle() {
      let list = this.fileList.filter((item) => !item.errorStatus);
      this.$emit("input", list);
    },
    handleError(res, file) {
      this.$load.close();
      const targetFile = this.fileList.find((item) => item.uid === file.uid);
      if (targetFile) {
        clearInterval(targetFile.interval);
        this.$set(targetFile, "errorStatus", true);
      } else {
        file.errorStatus = true;
        file.fileName = file.name;
        this.fileList.push(file);
      }
      this.$message.error(this.$t("msg.uploadFailed"));
    },
  },
};
</script>

<style lang="scss" scoped>
.upload-list-layout {
  height: 100%;
  width: 100%;
}
::v-deep.upload-wrap {
  font-size: 0;
  .el-upload-dragger {
    border-radius: 8px;
    background-color: #f3f3f3;
    border: 1px solid #dcdcdc;
    width: 100% !important;
    height: auto;
  }
  .el-upload__text {
    width: 272px;
    height: 154px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    font-size: 14px;
    span {
      margin-left: 8px;
      color: rgba(#000, 0.4);
      font-weight: 400;
      font-size: 14px;
    }
  }
}
</style>
